package com.example.truyencoandersen.fragments;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.truyencoandersen.FragmentChangeActivity;
import com.example.truyencoandersen.R;

public class BiaTruyenFragment extends Fragment {
	private int truyen_index = -1;
	private String[] menus;

	public BiaTruyenFragment() {
		this(0);
	}

	public BiaTruyenFragment(int index) {
		truyen_index = index;
		setRetainInstance(true);
	}

	@SuppressLint("NewApi")
	private Bitmap loadBitmap(Context context, String content, int width, int height) {
		Bitmap txtBitmap = Bitmap.createBitmap(width, height,
				Bitmap.Config.ARGB_8888);
		Canvas c = new Canvas(txtBitmap);
		
		View mView = getActivity().getLayoutInflater().inflate(
				R.layout.bia_truyen, null, false);
		
		TextView tv = new TextView(context);

		Drawable bg = getResources().getDrawable(R.drawable.bg_story);
		tv.setBackground(bg);

		tv.setText(content);
		tv.setTextColor(Color.BLACK);
		tv.setTextSize(50);
		tv.setLinksClickable(true);
		tv.setLineSpacing(2f, 1.5f);
		
		Typeface mFont = Typeface.createFromAsset(getActivity().getAssets(),
				"JOKERMAN.TTF");
		tv.setTypeface(mFont, Typeface.BOLD);
		tv.layout(10, 0, getResources().getDisplayMetrics().widthPixels,
				getResources().getDisplayMetrics().heightPixels);
		tv.draw(c);

		c.drawBitmap(txtBitmap, 0, 0, null);

		return txtBitmap;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		menus = getActivity().getApplicationContext().getResources()
				.getStringArray(R.array.menu_truyen);

		if (savedInstanceState != null)
			truyen_index = savedInstanceState.getInt("index");

		View v = inflater.inflate(R.layout.bia_truyen, container, false);
		ImageView image = (ImageView) v.findViewById(R.id.image);

		image.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (truyen_index == -1) {
					((FragmentChangeActivity) getActivity()).toggle();
				} else {
					getFragmentManager()
							.beginTransaction()
							.replace(R.id.content_frame,
									new TruyenFragment(truyen_index)).commit();
				}
			}
		});

		int rawWidth = 0;
		int rawHeight = 0;

		try {
			DisplayMetrics metrics = getActivity().getResources()
					.getDisplayMetrics();

			rawWidth = metrics.widthPixels;
			rawHeight = metrics.heightPixels;
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (truyen_index >= 0) {
			switch (truyen_index) {
			default:
				image.setImageBitmap(loadBitmap(getActivity(), menus[truyen_index], rawWidth,
						rawHeight));
				break;
			}
		}

		return v;
	}
}
